#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>

int main()
{
    int fd = open("tmp", O_CREAT | O_RDWR, 0664);
    if(-1 == fd) {
        perror("open error");
        exit(1);
    }

    pid_t pid = fork();
    if(-1 == pid) {
        perror("fork error");
        exit(1);
    }

    if(pid > 0) {
        char* p = "Hello Kitty!";
        write(fd, p, strlen(p)+1);
        close(fd);
    } else if(pid == 0) {
        // 延迟一秒，保证父进程完成写文件
        sleep(1);
        char buf[1024];

        lseek(fd, 0, SEEK_SET);
        int len = read(fd, buf, sizeof(buf));
        printf("len = %d, buf = %s\n", len, buf);
        close(fd);
    }

    return 0;
}

